package com.zxn.dorm.view.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.apkfuns.logutils.LogUtils;
import com.zxn.divider.LinearDecoration;
import com.zxn.divider.RvItemDecoration;
import com.zxn.dorm.ICommonEventKey;
import com.zxn.dorm.MjyApp;
import com.zxn.dorm.R;
import com.zxn.dorm.entity.MemberBeanInfo;
import com.zxn.dorm.view.adapter.MemberManageAdapter;
import com.zxn.dorm.view.dialog.AddDormMemberDgFrag;
import com.zxn.presenter.model.CommonEvent;
import com.zxn.presenter.view.BaseActivity;
import com.zxn.titleview.TitleView;
import com.zxn.utils.UIUtils;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zxn on 2020-1-1 21:50:08.
 */
public class DormListActivity extends BaseActivity {

    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.title_common)
    TitleView titleCommon;
    @BindView(R.id.rv_common)
    RecyclerView rvCommon;
    private String mParam1;
    private List<MemberBeanInfo> memberBeanInfoList;
    private MemberManageAdapter mAdapter;


    public static void jumpTo(Context context, String param1) {
        Intent intent = new Intent(context, DormListActivity.class);
        intent.putExtra(ARG_PARAM1, param1);
        context.startActivity(intent);
    }

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, DormListActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_dorm_list;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mParam1 = getIntent().getStringExtra(ARG_PARAM1);

        TextView view = new TextView(this);
        view.setText("添加");
        view.setTextColor(UIUtils.getColor(R.color.c_ffffff));
        view.setOnClickListener(v -> {
            AddDormMemberDgFrag.newInstance(memberBeanInfo -> {
                MjyApp.mMjyApp.mMemberBeanInfoDao.insert(memberBeanInfo);
                memberBeanInfoList = MjyApp.mMjyApp.mMemberBeanInfoDao.loadAll();
                mAdapter.setNewData(memberBeanInfoList);
            }).show(getSupportFragmentManager());
        });
        titleCommon.addRightView(view);


        rvCommon.setLayoutManager(new LinearLayoutManager(this));
        rvCommon.setHasFixedSize(true);
        rvCommon.addItemDecoration(new RvItemDecoration.Builder(this)
                .bgColor(R.color.c_edf4f8)
                .widthDp(10f)
                .setOrientation(LinearDecoration.VERTICAL)
                .createLinear());
        mAdapter = new MemberManageAdapter();
        rvCommon.setAdapter(mAdapter);

        initData();

        mAdapter.setNewData(memberBeanInfoList);
//        mAdapter.setOnItemClickListener((adapter, view, position) -> {
//            MemberEditActivity.jumpTo(mContext,memberBeanInfoList.get(position).getId());
//        });

        mAdapter.setOnDeleteItemListener(new MemberManageAdapter.OnDeleteItemListener() {
            @Override
            public void onItemDeleteClick(int position) {
                //long id = mAdapter.getData().get(position).getId();
                long id = memberBeanInfoList.get(position).getId();
//                LocalPrinterDao.deleteById(id);
                //GreenDB.getInstance(mContext).deletePrinterByKey(id);
                MjyApp.mMjyApp.mMemberBeanInfoDao.deleteByKey(id);
                mAdapter.getData().remove(position);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onItemClick(int position) {
                //NetPrinterDetailsActivity.jumpTo(NetPrinterActivity.this, mAdapter.getItem(position));
                MemberEditActivity.jumpTo(mContext, memberBeanInfoList.get(position).getId());
            }
        });

    }

    private void initData() {
        memberBeanInfoList = MjyApp.mMjyApp.mMemberBeanInfoDao.loadAll();
        if (null == memberBeanInfoList || memberBeanInfoList.isEmpty()) {
            String[] names = getResources().getStringArray(R.array.name_array);
            MjyApp.mMjyApp.mMemberBeanInfoDao
                    .insertInTx(new ArrayList<MemberBeanInfo>() {
                        {
                            for (int i = 0; i < names.length; i++) {
                                Long id = i + 10L;
                                int sex = 0;
                                add(new MemberBeanInfo(id, names[i], "1891079520" + i, sex));
                            }
                        }
                    });

            memberBeanInfoList = MjyApp.mMjyApp.mMemberBeanInfoDao.loadAll();
        }
    }

    @Subscribe
    public void onEvent(CommonEvent event) {
        LogUtils.i("onEvent:" + event.what);
        if (event.equals(ICommonEventKey.EVENT_UPDATE)) {
            memberBeanInfoList = MjyApp.mMjyApp.mMemberBeanInfoDao.loadAll();
            mAdapter.setNewData(memberBeanInfoList);
        }
    }

    @Override
    protected boolean usedEventBus() {
        return true;
    }
}
